package cn.xscrum.vtsp.controller;

import java.util.ArrayList;
import java.util.List;

import cn.xscrum.vtsp.domain.EnrollVolunteer;
import cn.xscrum.vtsp.domain.VtspPost;
import cn.xscrum.vtsp.domain.VtspVolunteer;
import cn.xscrum.vtsp.service.IVtspActivityService;
import cn.xscrum.vtsp.service.IVtspPostService;
import cn.xscrum.vtsp.service.IVtspVolunteerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.xscrum.common.annotation.Log;
import cn.xscrum.common.enums.BusinessType;
import cn.xscrum.vtsp.domain.VtspEnroll;
import cn.xscrum.vtsp.service.IVtspEnrollService;
import cn.xscrum.common.core.controller.BaseController;
import cn.xscrum.common.core.domain.AjaxResult;
import cn.xscrum.common.utils.poi.ExcelUtil;
import cn.xscrum.common.core.page.TableDataInfo;

/**
 * 报名信息Controller
 *
 * @author xscrum
 * @date 2021-10-01
 */
@Controller
@RequestMapping("/vtsp/enroll")
public class VtspEnrollController extends BaseController
{
    private String prefix = "vtsp/enroll";

    @Autowired
    private IVtspEnrollService vtspEnrollService;
    @Autowired
    private IVtspActivityService vtspActivityService;
    @Autowired
    private IVtspVolunteerService vtspVolunteerService;
    @Autowired
    private IVtspPostService vtspPostService;
    @Autowired
    private IVtspPostService postService;

    @RequiresPermissions("vtsp:enroll:view")
    @GetMapping()
    public String enroll()
    {
        return prefix + "/enroll";
    }

    /**
	 * 查询报名信息列表
     */
    @RequiresPermissions("vtsp:enroll:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(VtspEnroll vtspEnroll)
    {
        startPage();
        List<VtspEnroll> list = vtspEnrollService.selectVtspEnrollList(vtspEnroll);
        return getDataTable(list);
    }

    @PostMapping("/list/{activityId}")
    @ResponseBody
    public TableDataInfo listV(VtspEnroll vtspEnroll)
    {
        startPage();
        List<VtspEnroll> list = vtspEnrollService.selectVtspEnrollList(vtspEnroll);
        List<EnrollVolunteer> volunteerList = new ArrayList<>();
        list.forEach(enroll->{
            Long volunteerId = enroll.getVolunteerId();
            VtspVolunteer vtspVolunteer = vtspVolunteerService.selectVtspVolunteerById(volunteerId);
            EnrollVolunteer enrollVolunteer = new EnrollVolunteer();
            BeanUtils.copyProperties(vtspVolunteer,enrollVolunteer);
            BeanUtils.copyProperties(enroll,enrollVolunteer);
            enrollVolunteer.setPostName(postService.selectVtspPostById(enroll.getPostId()).getPostName());
            volunteerList.add(enrollVolunteer);
        });
        System.err.println(volunteerList);
        return getDataTable(volunteerList);
    }
    /**
     * 更换岗位
     */
    @GetMapping("/change/{enrollId}/{postId}")
    public String change(@PathVariable Long enrollId, @PathVariable Long postId, ModelMap mmap) {
        VtspPost post = postService.selectVtspPostById(postId);
        List<VtspPost> postList = postService.selectVtspPostList(null);
        mmap.addAttribute("enrollId", enrollId);
        mmap.addAttribute("postName", post.getPostName());
        mmap.addAttribute("postList", postList);
        return prefix + "/change";
    }
    /**
     * 添加成员
     */
    @GetMapping("/member/{activityId}")
    public String addMember(@PathVariable Long activityId, ModelMap mmap) {
        List<VtspVolunteer> volunteers = vtspVolunteerService.selectVtspVolunteerList(null);
        List<VtspPost> postList = postService.selectVtspPostList(null);
        mmap.addAttribute("activityId", activityId);
        mmap.addAttribute("volunteers", volunteers);
        mmap.addAttribute("postList", postList);
        return prefix + "/addMember";
    }

    /**
	 * 导出报名信息列表
     */
    @RequiresPermissions("vtsp:enroll:export")
    @Log(title = "报名信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(VtspEnroll vtspEnroll)
    {
        List<VtspEnroll> list = vtspEnrollService.selectVtspEnrollList(vtspEnroll);
        for (VtspEnroll vtspEnrollItem : list) {
            vtspEnrollItem.setAName(vtspActivityService.selectVtspActivityById(vtspEnrollItem.getActivityId()).getActivityName());
            vtspEnrollItem.setVName(vtspVolunteerService.selectVtspVolunteerById(vtspEnrollItem.getVolunteerId()).getVolunteerName());
            vtspEnrollItem.setPName(vtspPostService.selectVtspPostById(vtspEnrollItem.getPostId()).getPostName());
        }
        ExcelUtil<VtspEnroll> util = new ExcelUtil<VtspEnroll>(VtspEnroll.class);
        return util.exportExcel(list, "报名情况");
    }

    /**
	 * 新增报名信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
	 * 新增保存报名信息
     */
    @RequiresPermissions("vtsp:enroll:add")
    @Log(title = "报名信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(VtspEnroll vtspEnroll)
    {
        return toAjax(vtspEnrollService.insertVtspEnroll(vtspEnroll));
    }

    /**
	 * 修改报名信息
     */
    @GetMapping("/edit/{enrollId}")
    public String edit(@PathVariable("enrollId") Long enrollId, ModelMap mmap)
    {
        VtspEnroll vtspEnroll = vtspEnrollService.selectVtspEnrollById(enrollId);
        mmap.put("vtspEnroll", vtspEnroll);
        return prefix + "/edit";
    }

    /**
	 * 修改保存报名信息
     */
    @RequiresPermissions("vtsp:enroll:edit")
    @Log(title = "报名信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(VtspEnroll vtspEnroll)
    {
        System.err.println(vtspEnroll + "================================================");
        return toAjax(vtspEnrollService.updateVtspEnroll(vtspEnroll));
    }


    /**
	 * 删除报名信息
     */
    @RequiresPermissions("vtsp:enroll:remove")
    @Log(title = "报名信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(vtspEnrollService.deleteVtspEnrollByIds(ids));
    }
}
